﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace JQueryControlToolkit
{
    public class JSFunction : JSObject
    {
        public JSFunction(params JSObject[] statements)
        {
            if (statements == null)
                Body = new JSList();
            else
                Body = new JSList(statements);
            Parameters = new List<string>();
        }

        public List<string> Parameters { get; set; }

        public JSList Body { get; set; }

        #region JSObject Members

        public string ToScript()
        {
            return Extensions.ToScript(this);
        }

        public void ToScript(TextWriter toScript)
        {
            toScript.Write("function(");
            bool isFirst = true;
            foreach (string parameterName in Parameters)
            {
                if (isFirst)
                    isFirst = false;
                else
                    toScript.Write(",");
                toScript.Write(parameterName);
            }
            toScript.Write("){");
            isFirst = true;
            foreach (JSObject statement in Body)
            {
                if (isFirst)
                    isFirst = false;
                else
                    toScript.WriteLine(";");
                statement.ToScript(toScript);
            }
            toScript.Write("}");
        }

        #endregion
    }
}
